/*
 * EnrollPayFailedPresenter.java
 * business
 *
 * Created by chentao on 2017/4/21.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.main.discovery.enrollpayfailed;

import android.app.Activity;
import com.yidiandao.business.data.model.ArticleModel;
import com.yidiandao.business.data.model.IArticleModel;
import com.yidiandao.business.data.response.QueryEnrollPayStateResponse;
import com.yidiandao.business.ui.main.discovery.enroll.EnrollActivity;
import com.yidiandao.business.ui.widget.view.SuperLoadingLayout;
import com.yidiandao.business.utils.ActivityStack;
import java.util.List;

/**
 * Presenter
 */
public class EnrollPayFailedPresenter implements EnrollPayFailedContract.Presenter {

    private EnrollPayFailedContract.View mPayAgainView;

    private String mRecId;

    private IArticleModel mArticleModel;

    private String mOnlinePayUrl;


    public EnrollPayFailedPresenter(EnrollPayFailedContract.View view, String recId) {
        mPayAgainView = view;
        mRecId = recId;
        mArticleModel = new ArticleModel();

        mPayAgainView.setPresenter(this);
    }


    @Override
    public void start() {

        // 来到支付失败页面时,不能返回报名页面
        List<Activity> activityStack = ActivityStack.getActivityStack();
        for (Activity activity : activityStack) {
            if (activity instanceof EnrollActivity) {
                activity.finish();
                break;
            }
        }

        mArticleModel.getEnrollPayState(mPayAgainView.getPagerTag(), mRecId,
            new IArticleModel.OnLoadResultListener<QueryEnrollPayStateResponse.Data>() {
                @Override
                public void onSuccess(QueryEnrollPayStateResponse.Data data, boolean emptyData, boolean hasMore) {
                    if (data.getPayState() == 0) {
                        mPayAgainView.refreshContent(SuperLoadingLayout.STATE_SUCCESS);
                        mPayAgainView.refreshMessage(data.getMessage());
                        mOnlinePayUrl = data.getUrl();
                    } else {
                        mPayAgainView.refreshContent(SuperLoadingLayout.STATE_ERROR);
                    }
                }


                @Override
                public void onFailed(String message) {
                    mPayAgainView.refreshContent(SuperLoadingLayout.STATE_ERROR);
                }
            });
    }


    @Override
    public String getUrl() {
        return mOnlinePayUrl;
    }


    @Override
    public String getRecId() {
        return mRecId;
    }
}
